package Server.ServerCore.Controller;

import Server.ServerCore.Model.Player.PlayerInstance;
import Server.ServerCore.Model.vo.CardResultStatusVO;
import Server.ServerCore.Repository.ChipsServiceImpl;
import Server.ServerCore.Service.DataPacks.packRequest.SigleBuyIns_requst;
import Server.ServerCore.Service.DataPacks.packRespose.Chips_reback_Response;
import Server.ServerCore.Service.DataPacks.packRespose.SigleBuyIns_response;
import Server.ServerCore.Service.Logic.HandleController;
import Server.ServerCore.Service.Logic.RegisterPack;
import Server.ServerCore.Service.service.PlayerManagerService;
import org.jboss.netty.buffer.ChannelBuffer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author :石头哥哥<br/>
 *         Project:21Server
 *         Date: 13-4-17
 *         Time: 上午3:21
 *         SingleBuyInsController==215
 *         bug
 *
 */
@Service
public class SingleBuyInsController extends HandleController {
    private static final long serialVersionUID = 4764777354733955790L;
    /**
     * 数据分解具体处理的函数
     *
     * @param buffer buffer
     * @param player player
     */
    @Override
    public void DispatchPack(ChannelBuffer buffer, PlayerInstance player) throws Exception {
//        if (!player.isCloseInsuInfo()){
            SigleBuyIns_requst sigleBuyInsRequst= (SigleBuyIns_requst) parseObject(buffer,SigleBuyIns_requst.class);
            int CircleUnitID=sigleBuyInsRequst.getCircleUnitID();
            CardResultStatusVO cardResultStatusVO=player.getDeckoOfCardsVOFastList()[CircleUnitID];
            if (cardResultStatusVO!=null){
                cardResultStatusVO.setDoubleChips(cardResultStatusVO.getChipsPoints()/2);
                cardResultStatusVO.setInsurance((byte) 1); //下保险flag,赌庄家黑杰克
                double last=player.getBargainingChip()-cardResultStatusVO.getChipsPoints()/2;
                player.setBargainingChip(last);
                PlayerManagerService.update(player.getGameRole(),last);

                //返回筹码额
                Chips_reback_Response rebackResponse=new Chips_reback_Response();
                rebackResponse.setChips(player.getBargainingChip());
                player.DirectSend(rebackResponse.getJsonData(2,7));
            }

            //回复
            SigleBuyIns_response sigleBuyInsResponse=new SigleBuyIns_response();
            if (player.getNowCrileUnitID()==CircleUnitID&&CircleUnitID!=5){
                int now= PlayerManagerService.getNextcircleUnitID(player,CircleUnitID);
                if (now<=4&&now>=0){
                    sigleBuyInsResponse.setNextcircleUnitID(now);
                    player.setNowCrileUnitID(now);
                    player.DirectSend(sigleBuyInsResponse.getJsonData(2,15));
                }
                if (now==-1){
                    now=player.getCircleUnitIDS().get(0);
                    sigleBuyInsResponse.setNextcircleUnitID(now);
                    player.setNowCrileUnitID(now);
                    player.DirectSend(sigleBuyInsResponse.getJsonData(2,9));
                }
            }
//        }
    }

    /**
     * 通过spring产生处理数据的controller，储存在数组中
     */
    @Override
    public void InitiController() {
        RegisterPack.GAME_CONTROLLERS[215]=this;
    }
}
